<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="../../layui/css/layui.css?v=1" media="all">
    <META HTTP-EQUIV="pragma" CONTENT="no-cache">
    <META HTTP-EQUIV="Cache-Control" CONTENT="no-cache, must-revalidate">
    <META HTTP-EQUIV="expires" CONTENT="0">
    <script src="../../js/jquery/3.5.1/jquery.js?v=1"></script>
    <script src="../../layui/layui.js?v=1" charset="utf-8"></script>
    <script src="../../js/config.js?v=1"></script>
    <script src="../../js/function.js?v=1"></script>
    <script src="../../js/public.js?v=1"></script>
    <link rel="stylesheet" href="../../css/public.css?v=1">
    <link rel="stylesheet" href="../../css/follow/contact_records.css?v=1">

</head>
<body>
<div class="container">
    <div class="search-box">
        <div class="table-title">
            <p>联系记录</p>
        </div>
        <div class="search-main">
            <form class="layui-form">
                <div class="layui-form-item">
                    <div class="layui-form-label">联系状态</div>
                    <div class="layui-input-inline">
 <!--                       <input type="checkbox" name="status" value="1" lay-skin="switch" lay-filter="switch" lay-text="已联系到|仍需跟进">-->
                        <div class="switch3" name="status" title="已联系到|仍需跟进" option="0|1|0" value="" checked></div>
                        <style>
                            .layui-switch3{width:181px;}
                            .layui-switch3 em:nth-child(1){width:90px;}
                            .layui-switch3 em{width:90px;}
                        </style>
                    </div>
                    <div class="layui-input-inline search-date">
                        <div class="layui-form-label">下次提醒时间</div>
                        <div class="layui-input-inline">
                            <input type="text" class="layui-input" name="remind_day"><span>天</span>
                            <input type="text" class="layui-input" name="remind_time"><span>小时后</span>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-form-label">联系备注</div>
                        <textarea class="layui-textarea" name="remark"></textarea>
                </div>
                <div class="layui-form-item">
                    <div class="layui-form-label">下次随访</div>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" name="next_followup" id="next_followup" autocomplete="off">
                    </div>
                    <div class="layui-input-inline">
                        <input type="radio" name="next_followup_radio" value="1" title="明天" lay-filter="next_followup_radio">
                        <input type="radio" name="next_followup_radio" value="2" title="7天后" lay-filter="next_followup_radio">
                        <input type="radio" name="next_followup_radio" value="3" title="1个月后" lay-filter="next_followup_radio">
                    </div>
                </div>
                <div class="layui-form-item btn-box">
                    <button type="button" lay-submit="" lay-filter="submit-btn">保存</button>
                    <!--<button type="reset">取消</button>-->
                    <button type="button" class="reset-btn">取消</button>
                </div>
            </form>
            <div class="clear"></div>
        </div>
    </div>
    <div class="table-body">
        <div class="table-title" style="border-bottom:1px solid #cccccc;">
            <div class="table-title-left">
                <p>联系记录</p>
            </div>
            <div class="clear"></div>
        </div>
        <form class="layui-form">
            <table class="layui-table" lay-even lay-skin="nob" style="border-top:1px solid #cccccc;margin:0;" id="list-table" lay-filter="list-table">
            </table>
        </form>
    </div>
    <!--<div class="pages-box">
        <div class="pages-box-right">
            <p>到第</p>
            <input type="text">
            <p>页</p>
            <button>前往</button>
        </div>
        <div class="layui-box layui-laypage layui-laypage-default">
            <a style="border-left:none;">末页</a>
        </div>
        <div class="pages" id="pages"></div>
        <div class="layui-box layui-laypage layui-laypage-default">
            <a style="border-right:none;">首页</a>
        </div>
        <div class="clear"></div>
    </div>-->
</div>
</body>
<script>
    $(document).ready(function () {
        var patient_id = getLocalStorage('patient_id');
        $('body',parent.document).find('.change-center').hide();
        $('body',parent.document).find('.hospital').css('margin-right','20px');

        //获取用户基本信息
        ajax_get(configUrl.patientInfo,{user_id:patient_id},(res)=>{
            console.log(res);
            patient_info = res.data;
            var mianbaoxieSex = '';
            if(patient_info.sex == 1){
                mianbaoxieSex = '女';
            }else{
                mianbaoxieSex = '男';
            }
            var iframe_title = '<a class="mianbaoxie" href="javascript:;" data-href="./follow/list.html" data-title="随访管理"><p style="color:#4777E7;display: inline;">随访管理</p></a> / '+'随访管理详情'+' <p style="color:#FE9987;display:inline;">'+patient_info.name+' | '+mianbaoxieSex+' | '+patient_info.age+'岁</p>';
            $('body',parent.document).find('#iframe-title').html(iframe_title);
        })

        layui.config({
            base:'../../layui/layui_exts/'
        }).extend({
                switch3: 'switch3/switch3'
            }
        ).use(['element', 'form', 'layer', 'laydate', 'laypage','table','switch3'], function () {
            var element = layui.element;
            var form = layui.form;
            var layer = layui.layer;
            var laydate = layui.laydate;
            var laypage = layui.laypage;
            var table = layui.table;
            var switch3 = layui.switch3;

            var now = new Date();
            var year = parseInt(now.getFullYear());
            var month = parseInt(now.getMonth()+1);
            var day = now.getDate();
            var min_date = year+'-'+month+'-'+day;
            //下次随访时间
            laydate.render({
                elem: '#next_followup',
                'min':min_date,
            });

            switch3.render({
                elem:'.switch3',
                done:function(val,item){
                    console.log(val)
                    params.status = val;
                    console.log(params)
                    if(val == 0){
                        $('.search-date').show();
                    }else{
                        $('.search-date').hide();
                    }
                }
            });


            form.on('switch(switch)',function(data){
                if(this.checked == true){
                    params.status = 1;
                    $('.search-date').hide();
                }else{
                    params.status = 0;
                    $('.search-date').show();
                }
            })
            form.on('radio(next_followup_radio)',function(data){
                var now = new Date();
                var year = parseInt(now.getFullYear());
                var month = parseInt(now.getMonth()+1);
                var day = now.getDate();
                //该月最大天数
                var dd = getDaysInMonth(year,month);
                var addDay = 0;
                var value = this.value;
                console.log(value);
                if(value == 1){
                    //加1天
                    addDay = 1;
                }else if(value == 2){
                    //加7天
                    addDay = 7;
                }else if(value == 3){
                    //加30天
                    addDay = 30;
                }
                day = day+addDay;
                console.log(day)
                if(day > dd){
                    month = month +1;
                    if(month >12){
                        year = year+1;
                        month = month-12;
                    }
                    day = day-dd;
                }

                var str = year+'-'+month+'-'+day;
                console.log(str)
                $('input[name=next_followup]').val(str);
            })

            //获取标签列表
            var followList = '';
            var params = {
                'hosp_id':getLocalStorage('hosp_id'),
                'dept_id':getLocalStorage('dept_id'),
                'user_id':patient_id,
                'visit_level':1,
                'is_remind':0,
                'next_followup':'',
                'status':0,
                'remind_day':0,
                'remind_time':0,
            }


            form.on('submit(submit-btn)',function(data){
                params.next_followup = data.field.next_followup;
                params.remind_day = data.field.remind_day;
                params.remind_time = data.field.remind_time;
                params.remark = data.field.remark;
                if(params.next_followup == ''){
                    layer.msg('请选择下次随访日期', {icon: 5, time: 1500});
                    return false;
                }
                var loadindex = layer.load();
                ajax_post(configUrl.saveContactRecord,params,(res)=>{
                    layer.close(loadindex);
                    if (res.status == 0) {
                        layer.msg('保存成功', {icon: 1, time: 1000}, function () {
                            window.location.href='./list.html';
                        });
                    } else {
                        layer.msg(res.msg, {icon: 5, time: 1500});
                    }
                })

            })

            $('.reset-btn').click(function(){
                window.location.href='./list.html';
            })

            $('#bitchSendSms').click(function(){
                console.log('bitchsendsms');
                var str = 0;
                var id = tag = '';
                $("input[name='layTableCheckbox']").each(function() {
                    if($(this).attr('checked')=='checked') {
                        str ++;
                    }
                });
                console.log(str)

            })
            getFollowList({hosp_id:params.hosp_id,user_id:params.user_id,dept_id:params.dept_id});

            //获取数据
            function getFollowList(params){
                ajax_get(configUrl.getRecordsList,params,(res)=>{
                    console.log(res);
                    if(res.status == 0) {
                        lists = res.data;
                        var listsTmp = [];
                        $.each(lists.data, function (index, item) {
                            str = {
                                "from_flg":item.from_flg == 0?'':'短信',
                                "create_at":item.created_at,
                                "name":item.name,
                                "status":item.status == 1?'已联系到':'仍需跟进',
                                "remark":item.remark,
                            };
                            listsTmp.push(str)
                        });
                        table.render({
                            elem: "#list-table",
                            cols: [[ //标题栏
                                {field: 'from_flg', title: '联系途径'},
                                {field: 'create_at', title: '联系时间'},
                                {field: 'name', title: '联系人'},
                                {field: 'status', title: '联系状态'},
                                {field: 'remark', title: '联系备注'},
                            ]]
                            , data: listsTmp
                            , page: true
                            , even: true
                        })
                    }
                });
            }

            function recordList(userId){
                console.log(userId);
            }
        });
        function getDaysInMonth(year,month){
            var temp = new Date(year,month,0);
            return temp.getDate();
        }
    });
</script>
</html>